﻿using Games.Common;
using System;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Games.ChallengeMemory
{
    class GameCell : Image
    {
        private static Color Dark = Color.BurlyWood;
        public static Color GetColor()
        {
            return Utils.GetRandomColor(512);
        }
        public int ID { get; set; }
        private Color lastColor = Color.Gold;
        public event EventHandler Tapped;

        public async Task ToggleBright(Color color, bool quick = false)
        {
            lastColor = color;
            if(!quick)
            {
                await this.ColorTo(this.BackgroundColor, lastColor, c => this.BackgroundColor = c, 125);
            }
            else
            {
                //this.BackgroundColor = color;
            }
        }
        public async Task ToggleDark(bool quick = false)
        {
            if(quick)
            {
                this.BackgroundColor = Dark;
            }
            else
            {
                await this.ColorTo(this.BackgroundColor, Dark, c => this.BackgroundColor = c, 125);
            }
        }
        public async Task ShowFlash()
        {
            await this.ColorTo(this.BackgroundColor, lastColor, c => this.BackgroundColor = c, 25);
            await this.ColorTo(this.BackgroundColor, Dark, c => this.BackgroundColor = c, 25);
        }

        public GameCell()
        {
            this.BackgroundColor = Dark;
            TapGestureRecognizer tapGesture = new TapGestureRecognizer { NumberOfTapsRequired = 1 };
            tapGesture.Tapped += (sender, args) =>
            {
                Tapped?.Invoke(this, EventArgs.Empty);
            };
            GestureRecognizers.Add(tapGesture);
        }
    }
}
